package cn.csdj.sism.service.impl;

import cn.csdj.sism.dao.StudentMapper;
import cn.csdj.sism.pojo.Student;
import cn.csdj.sism.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 李心怡
 * 2023/4/10
 */
@Service
@Transactional
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public int insert(Student student) {
        studentMapper.insert(student);
        Integer studentNumber = 1000+student.getStudentId();
        student.setStudentNumber(String.valueOf(studentNumber));
        return studentMapper.updateById(student);
    }

    @Override
    public int deleteById(Integer id) {
        return studentMapper.deleteById(id);
    }

    @Override
    public int update(Student student) {
        return studentMapper.updateById(student);
    }

    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectList(null);
    }

    /**
     * 分页查询
     *
     * @param current 当前页码
     * @param pageSize 页面大小
     * @return
     */
    public IPage<Student> selectPages(Integer current,Integer pageSize) {
//        Page(当前页码，页面大小);
        IPage<Student> page = new Page(current,pageSize);
        studentMapper.selectPage(page,null);
        return page;//返回数据
    }

    /**
     * 按班级分页查询
     * @param current 当前页码
     * @param pageSize 页面大小
     * @return
     */
    public IPage<Student> selectPagesByClassId(Integer current,Integer pageSize,Integer classId) {
//        Page(当前页码，页面大小);
        IPage<Student> page = new Page(current,pageSize);
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Student::getClassId,classId);//查询对应班级id的学生，并分页
        studentMapper.selectPage(page,lqw);
        return page;//返回数据
    }

    /**
     * 多条件分页查询
     * @param condition 条件对象
     * @return
     */
    public IPage<Student> selectPagesByCondition(HashMap condition) {
//        Page(当前页码，页面大小);
        IPage<Student> page = new Page((Integer) condition.get("current"), (Integer) condition.get("pageSize"));
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(condition.get("studentName"))){
            lqw.like(Student::getStudentName,condition.get("studentName"));
        }
        if (!StringUtils.isEmpty(condition.get("studentPhone"))){
            lqw.eq(Student::getStudentPhone,condition.get("studentPhone"));
        }
        if (!StringUtils.isEmpty(condition.get("classId"))){
            lqw.eq(Student::getClassId,condition.get("classId"));
        }
        if (!StringUtils.isEmpty(condition.get("studentStatus"))){
            lqw.eq(Student::getStudentStatus,condition.get("studentStatus"));
        }
        studentMapper.selectPage(page,lqw);
        return page;//返回数据
    }

}
